/**
 * 定义app 定义路由
 */
var vshopApp = angular.module('vshopApp',['ngRoute']);

vshopApp.factory('tokenInterceptor', ['$location',function($location) {
    var tokenInterceptor = {
        request: function(config) {
        	token = localStorage.getItem("token");
        	console.log(token);
        	if(token == null){
        		$location.path("login");
        	}
        	config.headers['Authorization'] = 'Bearer '+localStorage.getItem("token");
            return config;
        },
    	responseError: function (response){
    		if(response.status=403){
    			$location.path("login");
    		}
    	}
    };
    return tokenInterceptor;
}]);

vshopApp.config(['$routeProvider','$httpProvider', function($routeProvider,$httpProvider) {
	$routeProvider.when('/login',{
		controller:'loginController',
		templateUrl:'views/login.html'
	}).when('/', { 
		controller: 'productController', 
		templateUrl: 'views/product.html', 
	}).when('/shoppingcart', {
		controller: 'shoppingcartController',
		templateUrl: 'views/shoppingcart.html',
	}).when('/service',{
		controller:'serviceController',
		templateUrl:'views/service.html'
	}).when('/usercenter',{
		controller:'usercenterController',
		templateUrl:'views/usercenter.html'
	}).when('/selectCustomer',{
		controller:'customerController',
		templateUrl:'views/selectCustomer.html'
	}).when('/order',{
		controller:'orderController',
		templateUrl:'views/order.html'
	});
	$httpProvider.interceptors.push('tokenInterceptor');
	
}]);
